/**
 * 隐患类型 编辑页
 *
 * 作者 wanghw
 * 日期 2024-08-29 15:10:21
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#hazardTypeOperation-edit'),
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (hazardTypeOperationData) {
            form.val("hazardTypeOperation-edit-form", {
                "typeOperationId": hazardTypeOperationData.typeOperationId,
                "typeName": hazardTypeOperationData.typeName,
                // "majorAccidentFlg": hazardTypeOperationData.majorAccidentFlg,
                "sort": hazardTypeOperationData.sort,
                "remark": hazardTypeOperationData.remark,
                // "status": hazardTypeOperationData.status,
            });
            if(hazardTypeOperationData.majorAccidentFlg == 1){
                $view.find('input[name=majorAccidentFlg]').attr("checked","true");
            }
            if(hazardTypeOperationData.status == 1){
                $view.find('input[name=status]').attr("checked","true");
            }
            form.render();
        }
    }

    form.on('submit(hazardTypeOperation-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        if(data.field.majorAccidentFlg == "on"){
            data.field.majorAccidentFlg = '1';
        }else{
            data.field.majorAccidentFlg = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('保存数据', '确定保存所编辑数据？', function () {
            if (data.typeOperationId && data.typeOperationId.length > 0) {
                febs.postArray(ctx + 'hazardTypeOperation/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-hazardTypeOperation').find('#query').trigger('click');
                    // febs.reloadPage();
                });
            } else {
                febs.postArray(ctx + 'hazardTypeOperation', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-hazardTypeOperation').find('#query').trigger('click');
                });
            }
        });

    }
});